package com.ymatou.iapi.livebuyer.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.livebuyer.parameter.GetLiveProductCountBean;
import com.ymatou.iapi.livebuyer.service.GetLiveProductCountCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.utils.EnvSetup;
/**
*查询直播商品总数Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetLiveProductCount{
	private static GetLiveProductCountBean getLiveProductCountBean;
	private static GetLiveProductCountCall getLiveProductCountCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询直播商品总数Test");
	}

	@Before
	public void caseUp() {
		getLiveProductCountBean=new GetLiveProductCountBean();
		getLiveProductCountCall=new GetLiveProductCountCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getLiveProductCountCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetLiveProductCount_001() {
		Logger.start(true,"正确数据，成功");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			List<String> ProductIdList = new ArrayList<>();
			int count = 10;
			for (int i = 0; i < count; i++) {
				String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
				
				Logger.comment("创建商品：" + pid);
				ProductIdList.add(pid);
			}
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), ProductIdList);
			Collections.shuffle(ProductIdList);
			LiveRoomCallService.SortLiveProduct(createlive.getId(), createlive.getUserId(), ProductIdList);
			
			Thread.sleep(2000);
			getLiveProductCountBean.setLiveIds(Arrays.asList(createlive.getId()));
			getLiveProductCountCall.setData(getLiveProductCountBean);
			getLiveProductCountCall.callService();			
			Logger.verifyEquals("200", getLiveProductCountCall.getString("code"), "验证code");
			JsonObject data = getLiveProductCountCall.getJsonObject().getAsJsonArray("data").get(0).getAsJsonObject();
			Logger.verifyEquals(createlive.getId(), data.get("liveId").getAsInt(), "验证直播Id");
			Logger.verifyEquals(ProductIdList.size(), data.get("liveProductCount").getAsInt(), "验证商品总数");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetLiveProductCount_002() {
		Logger.start(true,"正确数据，成功");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			List<String> ProductIdList = new ArrayList<>();
			int count = 40;
			for (int i = 0; i < count; i++) {
//				AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(uinfo.getUserId());
//				String pid =ProductManagerCallServiceV3.AddSellerProduct(addpro);
				String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
				Logger.comment("创建商品：" + pid);
				ProductIdList.add(pid);
			}
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLiveByAdmin(createlive.getId(), createlive.getUserId(), ProductIdList);
			
			Collections.shuffle(ProductIdList);
			LiveRoomCallService.SortLiveProduct(createlive.getId(), createlive.getUserId(), ProductIdList);
			
			Thread.sleep(2000);
			getLiveProductCountBean.setLiveIds(Arrays.asList(createlive.getId()));
			getLiveProductCountCall.setData(getLiveProductCountBean);
			getLiveProductCountCall.callService();			
			Logger.verifyEquals("200", getLiveProductCountCall.getString("code"), "验证code");
			JsonObject data = getLiveProductCountCall.getJsonObject().getAsJsonArray("data").get(0).getAsJsonObject();
			Logger.verifyEquals(createlive.getId(), data.get("liveId").getAsInt(), "验证直播Id");
			Logger.verifyEquals(ProductIdList.size(), data.get("liveProductCount").getAsInt(), "验证商品总数");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}